const { User, validateUser } = require("../../model/User")
const bcrypt = require('bcrypt');
module.exports = async (req, res, next) => {
    try {
        await validateUser(req.body)
    } catch(e) {
        return next(JSON.stringify({path: "/admin/user-edit", message: e.message}))
    }

    let user = await User.findOne({email: req.body.email})
    // 判断邮箱是否占用
    if (user) {
        return next(JSON.stringify({path: "/admin/user-edit", message: "邮箱地址已经被占用"}))
    }
    // 为密码进行加密
    const salt = await bcrypt.genSalt(10);
    const password = await bcrypt.hash(req.body.password, salt)
    req.body.password = password
    await User.create(req.body)
    res.redirect('/admin/user')
}